package com.reader.cms.service.impl.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.reader.cms.entity.sys.SysOrganization;
import com.reader.cms.mapper.sys.SysOrganizationMapper;
import com.reader.cms.service.sys.ISysOrganizationService;
import com.reader.core.entity.ResultMessageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @@description  服务实现类
 * @author        jiahaikun
 * @date          2018-08-06
 */
@Service
@Transactional
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {


@Autowired
private SysOrganizationMapper sysOrganizationMapper;

    /**
     * 递归获取子组织机构
     * @param allorganizations  机构集合
     * @param organization 父机构
     */
    private void getOrganizeChild(List<SysOrganization> allorganizations, SysOrganization organization) {
        List<SysOrganization> organizations = allorganizations.stream().filter(a -> a.getParentCode().equals(organization.getCode())).collect(Collectors.toList());
        if(organizations.size() != 0) {
            organization.setChildren(organizations);
            for (SysOrganization sysOrganization : organizations) {
                getOrganizeChild(allorganizations, sysOrganization);
            }
        }
    }


    /**
     * @function 多表关联查询
     * @param     currentPage 当前页
     * @param     size 每页显示条数
     * @param     parms 其它参数，自行定义
     * @return
     */
    @Transactional(readOnly=true)
    @Override
    public Page<Map<String, Object>> selectListPage(int currentPage,int size,String parms) {
            Page<Map<String, Object>> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
            return page.setRecords(sysOrganizationMapper.selectListPage(page,parms));
    }

    @Override
    public List<SysOrganization> getOrganizeTree(String name, String code) {
        List<SysOrganization> allorganizations = sysOrganizationMapper.getAllOrganizations(name,code);
        List<SysOrganization> rootOrganizations = allorganizations.stream().filter(a -> StringUtils.isBlank(a.getParentCode())).collect(Collectors.toList());
        for (SysOrganization organization : rootOrganizations) {
            getOrganizeChild(allorganizations, organization);
        }
        if(rootOrganizations.size() == 0){
            rootOrganizations = allorganizations;
        }
        return rootOrganizations;
    }

    @Override
    public List<Integer> getOrganizeRoles(Integer id) {

        return sysOrganizationMapper.getOrganizeRoles(id);
    }

    @Override
    public void insertOrganization(SysOrganization sysOrganization) {
        Integer insert = sysOrganizationMapper.insert(sysOrganization);
        if(insert == 1){
            if(sysOrganization.getRoleIds().size() != 0){
                sysOrganizationMapper.insertOrganizationRole(sysOrganization.getId(),sysOrganization.getRoleIds());
            }

        }

    }

    @Override
    public void updateOrganize(SysOrganization sysOrganization) {
        Integer integer = sysOrganizationMapper.delSysOrganizationRole(sysOrganization.getId());
        Integer result = sysOrganizationMapper.updateById(sysOrganization);
        if(result == 1){
            if(sysOrganization.getRoleIds().size() != 0){
                sysOrganizationMapper.insertOrganizationRole(sysOrganization.getId(),sysOrganization.getRoleIds());
            }
        }
    }

    @Override
    public ResultMessageInfo deleteOrganize(SysOrganization sysOrganization) {
        Integer count = sysOrganizationMapper.getUserOfOrganize(sysOrganization.getId());
        if(count > 0){
            return  ResultMessageInfo.ofMessage("机构正在使用不能删除！！！");
        }
        Integer integer = sysOrganizationMapper.delSysOrganizationRole(sysOrganization.getId());
        sysOrganizationMapper.updateById(sysOrganization);
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}